<?php
class SetupController extends AppController {
	var $name = 'Setup';
	var $uses = array('User','Cms');
	var $permissions = array(
		'add_manag' => array('admin','manag'),
		'index' => array('admin','manag'),
		'list_manag' => array('admin','manag'),
		'edit_manag' => array('admin','manag'),
		'delete_manag' => array('admin','manag'),
		'backup'	=> array('admin','manag')
	);	
	function index() {

	}
	
	function add_manag() {
		if (!empty($this->data)) {
			$this->data['User']['email'] = $this->data['Setup']['email'];
			$this->data['User']['password'] = $this->Auth->password($this->data['Setup']['password']);	
			$this->data['User']['status'] = 1;
			$this->data['User']['group'] = 'manag';				
			$this->User->create();
			if ($this->User->save($this->data)) {
				$this->data['Employee']['email'] = $this->data['Setup']['email'];
				$this->data['Employee']['password'] = $this->data['User']['password'];
				$this->data['Employee']['user_id'] = $this->User->getLastInsertID();	
			
				$this->User->Employee->create();
				if($this->User->Employee->save($this->data)){
					$this->Session->setFlash(__("The manager has been saved", true), 'flash_succes');
				}else{
					$this->Session->setFlash(__("The manager could not be saved. Please, try again.", true), 'flash_error');
				}				
				$this->redirect(array('action'=>'list_manag'));
			} else {
				$this->Session->setFlash(__("The manager could not be saved. Please, try again.", true), 'flash_error');
			}				
		}
	}

	function list_manag(){
		$this->User->recursive = 0;
		$manag = $this->paginate(null,array('User.group'=>'manag'));
		$this->set('managers',$manag);
	}
	
	function edit_manag($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__("Invalid manager id", true));
			$this->redirect(array('action' => 'list_manag'));
		}
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->data['Employee'] = $this->data['Employee']['0'];
				if ($this->User->Employee->save($this->data)) {
					$this->Session->setFlash(__("The manager has been saved", true));
				}
				$this->redirect(array('action' => 'list_manag'));
			} else {
				$this->Session->setFlash(__("The manager could not be saved. Please, try again.", true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
	}
	
	function delete_manag($id = null) {
		if (!$id) {
			$this->Session->setFlash(__("Invalid id for manager", true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__("Manager deleted", true));
			$this->redirect(array('action'=>'list_manag'));
		}
		$this->Session->setFlash(__("Manager was not deleted", true));
		$this->redirect(array('action' => 'list_manag'));
	}
	
	function backup(){
		$file = 'files/'.HOST.'/database_backup_'.date(' YmdHis ').'.gz';
		file_put_contents($file, gzencode( file_get_contents('../database/'.HOST.'/default.'.HOST.'.db'),9));

		$this->Email->to = $this->Session->read("Auth.User.email");  
	    $this->Email->subject = __("Database Backup",true);
	    $this->Email->from = "Administrator <{$this->Session->read("Auth.User.email")}>";		
		$this->Email->attachments = array(  $file  );
//		$this->Email->sendAs = 'both'; // because we like to send pretty mail
//		$this->Email->delivery = 'debug';
	    $this->Email->send();	
	    	
	    $this->Session->setFlash(__("Database backup has been saved", true));
		$this->redirect(array('action'=>'index'));
	}
}
?>